//
//  RegisterViewController.m
//  JxCarios
//
//  Created by xcq on 15/1/27.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterPassWordViewController.h"


@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
}

- (void)setUp {
    if (_userName.text.length <= 0) {
        
        [_randBtn setEnabled:NO];
        [_nextBtn setEnabled:NO];
    }
     _randCode.delegate = self;
    _randCode.tag = 201;
    _userName.delegate = self;
    _userName.tag = 200;
    if (_status == 2) {//找回密码
        self.title = @"找回密码";
        [_nextBtn setTitle:@"验证" forState:UIControlStateNormal];
        [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    }else{
        self.title = @"用户注册";
        [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
        
    }
}

- (void)statusChange:(NSInteger)statue{
    _status = statue;
  
 }

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    if (toBeString.length == 0) {
        [_randBtn setEnabled:NO];
        [_nextBtn setEnabled:NO];
        [_randBtn setBackgroundColor:[UIColor lightGrayColor]];
        [_nextBtn setBackgroundColor:[UIColor lightGrayColor]];
        [_randTimer invalidate];
    }else{
        if (!_bol) {
            [_randBtn setEnabled:YES];
            [_nextBtn setEnabled:YES];
            [_randBtn setBackgroundColor:BUTTUN_COLOR];
            [_nextBtn setBackgroundColor:BUTTUN_COLOR];
            
        }
    }
    if (textField.tag == 200) { //账号输入
        //只能输入数字
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:Point]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
        BOOL canChange = [string isEqualToString:filtered];
        return canChange;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



- (IBAction)nextAction:(UIButton *)sender {
    RegisterPassWordViewController *registe = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"registerPass"];
    registe.status = _status;
    registe.randCode = [_randCode.text doubleValue];
    registe.mobile = _userName.text;
    [self.navigationController pushViewController:registe animated:YES];
    [self.randTimer invalidate];
    self.randTimer = nil;
    [self.randBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.randBtn.enabled = YES;
    self.randBtn.backgroundColor = UICOLOR_BUTTON;
    
}
-(void) randTimerCallback
{
    int time =  [[NSDate date] timeIntervalSince1970] - [[self.randTimer userInfo] timeIntervalSince1970];
    if (time < 60) {
        self.randBtn.enabled = NO;
        [self.randBtn setTitle:[NSString stringWithFormat:@"%d秒后重新发送",60 - time] forState:UIControlStateNormal];
        self.randBtn.backgroundColor = [UIColor grayColor];
        _bol = YES;
    }else{
        _bol = NO;
        [self.randTimer invalidate];
        self.randTimer = nil;
        [self.randBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.randBtn.enabled = YES;
        self.randBtn.backgroundColor = UICOLOR_BUTTON;
    }
}

- (NSTimer *)randTimer
{
    if (!_randTimer) {
        _randTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(randTimerCallback) userInfo:[NSDate date] repeats:YES];
        [_randTimer fire];
    }
    
    return _randTimer;
}
//获取验证码
- (IBAction)randAction:(id)sender {
    [self.view endEditing:YES];
    if (_userName.text.length <= 0) {
        //TODO:用户名错误的提示信息
        [self.view makeToast:@"请输入用户名"];
        return;
    }
    if (!self.dataSource) {
        self.dataSource = [CurrentDataSource dataSourceWithRequestor:self];
    }
    
    self.dataSource.userName = _userName.text;
    [self.dataSource getRand:^(id result) {
        [self.navigationController.view makeToast:@"验证码已发送" duration:2 position:TOP_POINT];
        [self randTimer];
    } andFail:^(id result) {
 
     }   withStatus:_status];
    
     
}
@end
